GBML manual / Grids

grid_get_region_max

Finds the biggest cell in a block of target - from column x1, row y1 to column x2, row y2, both corners included - and gives its value. A block of only negative numbers gives the biggest of them, not 0. A block with nothing in it gives 0. A grid of text, yes-or-no values or structs is refused when the project is checked.

Syntax

grid_get_region_max(target, x1, y1, x2, y2) -> element

Arguments

NameType
targetgrid
x1int
y1int
x2int
y2int

Returns

element

Description

Finds the biggest cell in a block of target, a grid, and gives you its value. The block runs from column x1, row y1 to column x2, row y2, and both corners are included: (1, 1) to (3, 2) is 6 cells.

On a grid of whole numbers the answer is a whole number. On a grid of decimals it is a decimal.

var best = grid_get_region_max(scores, 0, 0, 3, 2);

The block follows the same rules as grid_fill_region, which has the whole story. In short: both corners are included, corners the wrong way round make an empty block, a block that runs off the grid is cut to the grid, and a corner typed as a number that is off the grid is refused when the project is checked.

The biggest of some negative numbers is not 0

The answer is always the biggest cell that is really in the block. A block holding -5, -2 and -9 gives -2, not 0. 0 is only the answer when the block has nothing in it.

A block with nothing in it gives 0

If the corners are the wrong way round, or the block is wholly off the grid, there are no cells to look at, and the answer is 0. This can only happen with numbers that are worked out while the game runs. A mistake typed straight into the code is refused when checked.

Because 0 is also a real answer (a block of zeros, or of -3 and 0), you cannot tell an empty block from a block whose biggest cell is 0 just by looking at the answer. If that matters, make sure the corners are the right way round before you ask.

It only works on numbers

grid_get_region_max needs a grid of whole numbers (grid_create(8, 6, 0)) or of decimals (grid_create(8, 6, 0.0)). A grid of text, of yes-or-no values, of structs or of references has no "biggest", so it does not build: the project is refused when it is checked, with a message that says what the grid holds. It is a message from GBML, not an error from the compiler later on.

The answer can never be too big for its kind, because it is one of the cells that was already there.

The grid has to know its size

Like grid_fill_region, it needs to know how many cells are in a row. A grid with no size written down, or a script's grid parameter, is refused. Do the looking in the object that made the grid and hand the script the answer.

What happens in the awkward cases

You do thisWhat happens
use it on a grid of text, yes-or-no, structs or referencesrefused when checked
give a corner with a decimal point, like 1.5refused when checked. A corner is a whole number
give it a list or an arrayrefused when checked
give it the wrong number of argumentsrefused when checked
a block that runs off the gridonly the cells on the grid are looked at

What it costs

It visits every cell in the block once, so the time grows with the number of cells in the block. It does not change the grid.

Example

var scores = grid_create(4, 3, 0);
scores[# 2, 1] = 7;
scores[# 3, 2] = 4;
var best = grid_get_region_max(scores, 0, 0, 3, 2);       // 7
var low = grid_get_region_max(scores, 3, 2, 3, 2);        // just one cell: 4

On the GBA

Two loops over the block, keeping the biggest cell seen so far - O(cells in the block). The first cell in the block is the starting point, so a block of negatives does not answer 0. It cannot overflow. It needs the grid's width, which a flat vector does not carry, so it takes the width and height the checker reads from the grid's declaration (`grid_create(8, 6, 0)`, `/// @capacity 8x6` or `grid<int, 8, 6>`) and writes them into the C++ as plain numbers - the same two numbers [# x, y] uses. A grid with none (a script's parameter, one declared with no shape) is refused. The four numbers are worked out once each, in the order written, then held to 0..width-1 and 0..height-1, so the loops cannot leave the grid and a block that runs off one edge never wraps onto the next row; the flat index is also checked against the vector's real size. Numbers typed straight into the call that are off the grid, or corners typed the wrong way round, are refused when the project is checked. Only int and real grids build.

See also: grid_get_region_sumgrid_get_region_meangrid_fill_regiongrid_widthgrid_heightgrid_createGridsData containers